using System.Threading;

namespace slcl.helpers
{
    public class AsyncRequest
    {
        /// <summary>
        /// Internal thread singalling.  Marked set when complete
        /// </summary>
        protected ManualResetEvent _CompletedSignal = new ManualResetEvent(false);

        /// <summary>
        /// Checks if current requested operation is complete
        /// </summary>
        public bool IsComplete
        {
            get
            {
                // Returns false if manual reset event not set.
                return _CompletedSignal.WaitOne(0, false);
            }

            set
            {
                _CompletedSignal.Set();
            }
        }

        /// <summary>
        /// Wait indefinitely for requested operation to complete
        /// </summary>
        public void Wait()
        {
            _CompletedSignal.WaitOne();
        }

        /// <summary>
        /// Wait a number of milliseconds for operation to complete
        /// </summary>
        /// <param name="timeout"></param>
        /// <returns>false if timed out, true otherwise</returns>
        public bool Wait(int timeout)
        {
            return _CompletedSignal.WaitOne(timeout, false);
        }
    }

}